//
//  SLPagerView.h
//  SLPagerView
//
//  Created by l.t.zero on 2022/1/7.
//

#import <UIKit/UIKit.h>
#import "SLPagerReusePool.h"
#import "SLPagerHelper.h"
#import "SLPagerLayout.h"

NS_ASSUME_NONNULL_BEGIN

//@protocol SLPagerCell
//
//@end

@class SLPagerView;
@protocol SLPagerViewDelete <NSObject>

- (void)sl_pagerView:(SLPagerView *)pagerView dequeueView:(id)cell atIndexPath:(NSIndexPath*)indexPath withData:(id<SLPagerItem>)item;

@optional
- (void)sl_pagerView:(SLPagerView *)pagerView prefetchView:(id)cell atIndexPath:(NSIndexPath*)indexPath withData:(id<SLPagerItem>)item;

@optional
- (NSString *)sl_pagerView:(SLPagerView *)pagerView identifierAtIndexPath:(NSIndexPath *)indexPath;

- (void)sl_pagerView:(SLPagerView *)pagerView willDisplay:(id)cell atIndexPath:(NSIndexPath*)indexPath withData:(id<SLPagerItem>)item;
- (void)sl_pagerView:(SLPagerView *)pagerView didEndDisplay:(id)cell atIndexPath:(NSIndexPath*)indexPath withData:(id<SLPagerItem>)item;
- (void)sl_pagerView:(SLPagerView *)pagerView didFrom:(SLPagerObject *)from to:(SLPagerObject *)to scrolling:(BOOL)isScrolling;

- (void)sl_pagerView:(SLPagerView *)pagerView didCancelledPrefetch:(id)cell atIndexPath:(NSIndexPath*)indexPath withData:(id<SLPagerItem>)item;

@end

@protocol SLPagerLifeCycle <NSObject>

@optional
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;

@end


typedef NS_ENUM(NSInteger, SLPagerViewDataPosition){
    SLPagerViewDataPositionAfter = 0,
    SLPagerViewDataPositionBefore
};


@interface SLPagerView : UIView<SLPagerLifeCycle>

@property (nonatomic, readonly) NSArray<id<SLPagerItem>> *dataSource;
@property (nonatomic, weak) id<SLPagerViewDelete> delegate;
@property (nonatomic, readonly) UICollectionView *collectionView;
@property (nonatomic, readonly) NSInteger currentIndex;
//@property (nonatomic, readonly) UICollectionViewLayout *layout;
@property (nonatomic, readonly) BOOL isScrolling;
@property (nonatomic) BOOL enableInfinateScroll;///default NO
@property (nonatomic) BOOL prefetchingEnabled;///default YES

- (instancetype)initWithFrame:(CGRect)frame layout:(UICollectionViewLayout *)layout;
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

- (void)scrollToIndex:(NSInteger)index animated:(BOOL)animated;
- (void)scrollToNextIndex;

/// 注册复用Cell类，配合[SLPagerViewDelete sl_pagerView:identifierAtIndexPath:]使用
/// 建议使用sl_pager_item_add_cell_for_class为dataSource的元素动态添加cellForClass来确定类型
/// dataSource = sl_array_map(dataList, ^(id obj) {
///    sl_pager_item_add_cell_for_class(obj, ^(id item) {
///        return [UICollectionViewCell class];
///    });
///    return obj;
///    });
/// @param cellClass 显示所用的Cell类型
/// @param identifier  复用ID，保证唯一
- (void)registerClassForReuse:(nullable Class)cellClass identifier:(NSString *)identifier;

///设置dataSource同时进行reloadData
- (void)reloadData NS_UNAVAILABLE;
- (void)setDataSource:(NSArray<id<SLPagerItem>> *)dataSource scrollToIndex:(NSInteger)index;
- (void)appendData:(NSArray *)data atPosition:(SLPagerViewDataPosition)position;
- (void)removeItem:(id)item animated:(BOOL)animated;

- (UIView*)viewAtIndex:(NSInteger)index;
- (NSIndexPath *)indexForView:(UIView *)view;

///监听指定的viewController，动态获取生命周期传递给self
///涉及方法：viewWillAppear/viewDidAppear/viewWillDisappear/viewDidDisappear
/// @param controller  被监听的controller
- (void)observeOnViewController:(UIViewController *)controller;

@end

NS_ASSUME_NONNULL_END
